//
//  TTISearchVC.m
//  ContentApp
//
//  Created by Jery on 2018/7/16.
//  Copyright © 2018年 翔傲科技. All rights reserved.
//

#import "TTISearchVC.h"

@interface TTISearchVC ()<UITextFieldDelegate>
{
    UIView *titleview;
    UITextField *textfield;
}
@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) IBOutlet UIView *deleteBgView;

@property (nonatomic,strong) NSMutableArray *hotWordsArr;

@end

@implementation TTISearchVC

#pragma mark -- 懒加载
- (NSMutableArray *)hotWordsArr
{
    if (!_hotWordsArr) {
        _hotWordsArr = [NSMutableArray array];
    }
    return _hotWordsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NAV_INIT(self, @"", [UIImage imageNamed:@"btn_back_nor"], @selector(backBtnClick), nil, nil);
    
    [self setupTitleView];
    [self initView];
    
    [self requestData];
    
    [self reloadScrollView];
    // Do any additional setup after loading the view.
}

- (void)initView
{
    self.deleteBgView.layer.borderColor = [UIColor grayColor].CGColor;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

- (void)setupTitleView
{
    titleview = [[UIView alloc] init];
    
    if (iPhone6P){
        titleview.size = CGSizeMake(UIScreenWidth - 155, 30);
    }else{
        titleview.size = CGSizeMake(UIScreenWidth - 145, 30);
    }
    titleview.backgroundColor = [UIColor redColor];
    
    // 背景图片
    UIImageView *bgimageview = [[UIImageView alloc] init];
    bgimageview.frame = CGRectMake(-20, 0, titleview.width + 80, titleview.height);
    bgimageview.image = [UIImage imageNamed:@"top_bg_search"];
    bgimageview.userInteractionEnabled = YES;
    [titleview addSubview:bgimageview];

    // 请输入搜索文字
    textfield = [[UITextField alloc] init];
    textfield.frame = CGRectMake(20, 0, titleview.width - 40, 30);
    textfield.placeholder = @"请输入关键字";
    textfield.font = [UIFont systemFontOfSize:13];
    textfield.returnKeyType = UIReturnKeySearch;
    //    [textfield becomeFirstResponder];
    textfield.delegate = self;
    [textfield addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    [bgimageview addSubview:textfield];
    self.navigationItem.titleView = titleview;

}

- (void)requestData
{
    NSArray *tempArr1 = [NSArray arrayWithObjects:@"推荐",@"纽崔莱",@"雅姿",@"家居科技",@"个人护理",@"海外购",@"美好生活", nil];
    [self.hotWordsArr addObjectsFromArray:tempArr1];
}

- (void)reloadScrollView
{
    
    float margin = 10.0f;
    float posX = 10.0f;
    for (int i = 0; i < self.hotWordsArr.count; i ++) {
        
        NSString *currentWord = [self.hotWordsArr objectAtIndex:i];
        
        CGSize titleSize = [currentWord sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
        
        float curWidth = (titleSize.width + 30) > 50 ? (titleSize.width + 30) : 50;
        
        UILabel * lab = [[UILabel alloc] init];
        lab.frame = CGRectMake(posX,15, curWidth, 30);
        lab.font = [UIFont systemFontOfSize:12];
        lab.backgroundColor = [UIColor redColor];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.layer.cornerRadius = 10.0f;
        lab.layer.masksToBounds = YES;
        lab.text = [NSString stringWithFormat:@"%@",currentWord];
        
        posX += curWidth + margin;
     
        [self.myScrollView addSubview:lab];
        
        
    }
    self.myScrollView.contentSize = CGSizeMake(posX, self.myScrollView.frame.size.height);
   
    
}


#pragma mark - UITextFieldDelegate
- (void)textDidChange:(UITextField *)textField
{
//    selectview.hidden = YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    [self searcBtnClick];
    return YES;
}


#pragma mark - event response
- (void)backBtnClick
{
    [textfield resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
